package boxoffice;

import java.awt.*;
import javax.swing.JPanel;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

/**
 * ImagePanel is a JPanel for the display of an Image.
 * Uses a BufferedImage to store the image.
 *
 * @author Dale Shpak
 * @version %I%, %G%
 * @since JDK 1.3
 */
public class ImagePanel extends JPanel {
    /** Image is loaded into this BufferedImage for display */
    private BufferedImage image = null;
    /** User can specify the size of the ImagePanel */
    private Dimension size;
 
    /**
     * Constructor with no args creates a 200 by 200 panel.
     *
     * @author Dale Shpak
     * @version %I%, %G%
     * @since JDK 1.3
     */	   
    public ImagePanel (String fileName) {
            this(fileName, null);
    }

    /**
     * Construct a panel of the specified size.
     *
     * @author Dale Shpak
     * @version %I%, %G%
     * @param sz Size of the ImagePanel in pixels.
     * @since JDK 1.3
     */	
    public ImagePanel (String fileName, Dimension sz) {  
        super(new BorderLayout());
        FileInputStream inputStream;
        try {
            inputStream = new FileInputStream (fileName);
            image = ImageIO.read(inputStream);
            inputStream.close();
        }
        catch (FileNotFoundException e) {
            System.out.println("Unable to find image file");
        }
        catch (IOException e) {
            System.out.println ("Unable to read image file");
        }
        
        setPanelSize(sz);
        setBackground (Color.WHITE);
    }
    
    /**
     * Sets the size of the panel that is displaying the image.
     *
     * @author Dale Shpak
     * @version %I%, %G%
     * @param sz The desired size of the ImagePanel
     * @since JDK 1.3
     */
    private void setPanelSize(Dimension sz) {
        size = sz;
        if (size != null) {
            setPreferredSize (size);
        } else {
            setPreferredSize (new Dimension (image.getWidth(), image.getHeight()));
        }
    }
    
    /**
     * Sets the current image to be displayed within the panel
     * panel to accomodate the image.
     *
     * @author Dale Shpak
     * @version %I%, %G%
     * @param im The buffered image to be displayed.
     * @since JDK 1.3
     */	
    public void setImage (BufferedImage im, Dimension sz) {
        image = im;
        if (image == null) return;
        size.setSize (image.getWidth(), image.getHeight());
        setPanelSize(sz);
    }
    
    /**
     * Overrides paintComponent() so that the image is displayed if it exists
     *
     * @author Dale Shpak
     * @version %I%, %G%
     * @since JDK 1.3
     */	
    public void paintComponent (Graphics g) {
        super.paintComponent (g);
        int x=0, y=0;
        
        /* Centre the image if it is smaller than the panel */
        if (this.getWidth() > image.getWidth()) x = (this.getWidth() - image.getWidth())/2;
        if (this.getHeight() > image.getHeight()) x = (this.getHeight() - image.getHeight())/2;
        
        Graphics2D g2D = (Graphics2D) g;
        if (image != null) {
            g2D.drawImage (image, null, x, y);
        }
    }
}